﻿using System;
using System.Web;
using SocialShare.BusinessLogicLayer;
using SocialShare.BusinessEntities;
using SocialShare.DAL.Facades;
using Utilities;
using SocialShare.BLL;
using System.Configuration;

public partial class admin_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void imbLogin_Click(object sender, EventArgs e)
    {
        try
        {
            
            string emailAddress = tbxEmailAddress.Value.Trim();
            string password = tbxPassword.Value.Trim();
            MemberFacade bll = new MemberFacade();
            SocialShare_Admin_Users user = bll.ValidateAdmin(emailAddress, password);
            if (user!=null)
            {
                CookieManager.AdminId = user.Admin_User_Id.ToString();
                CookieManager.IsAdmin = true;
                CookieManager.IsLoggedout = false;
                CookieManager.UserName = emailAddress;
                HttpCookie cookie = System.Web.Security.FormsAuthentication.GetAuthCookie(emailAddress, chkRememberMe.Checked);
                // Extract the ticket
                System.Web.Security.FormsAuthenticationTicket ticket = System.Web.Security.FormsAuthentication.Decrypt(cookie.Value);
                // Create the new identity for this request
                System.Web.Security.FormsIdentity identity = new System.Web.Security.FormsIdentity(ticket);
                // Apply it.
                Context.User = new System.Security.Principal.GenericPrincipal(identity, new string[]{"admin"});
                Context.Response.Cookies.Add(cookie);
                System.Web.Security.FormsAuthentication.RedirectFromLoginPage(emailAddress, chkRememberMe.Checked);
                if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["ReturnUrl"].ToLower()))
                {
                    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.QueryString["ReturnUrl"]);
                }
                else
                {
                    HttpContext.Current.Response.Redirect("~/admin/manage-users.aspx");
                }
                
            }
            else
            {
                ConfigReader configReader = new ConfigReader();
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.LOGIN_FAILED.ToString());
            }
        }
        catch (Exception ex)
        {

        }
    }
}